package cn.shyouchi.wuguanjia.android.api.casual;

import android.content.Context;

import com.alibaba.fastjson.JSON;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.shyouchi.wuguanjia.android.api.ClientProvider;
import cn.shyouchi.wuguanjia.android.api.UserStorageProvider;
import cn.shyouchi.wuguanjia.android.components.storage.UserStorage;
import cn.shyouchi.wuguanjia.android.model.BaseRequestResult;
import cn.shyouchi.wuguanjia.android.model.BaseResult;
import cn.shyouchi.wuguanjia.android.ui.casualrecord.ChooseGroup;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import retrofit2.Retrofit;
import rx.Observable;
import rx.functions.Func1;
import rx.schedulers.Schedulers;
import top.zibin.luban.Luban;

/**
 * Created by Eenie on 2017/3/22 at 16:33
 * Email: 472279981@qq.com
 * Des:
 */

public class CasualApi {


    private CasualService mService;

    private UserStorage mUserStorage;
    private Context mContext;


    public CasualApi(Context context) {
        this.mContext = context;
        mUserStorage = UserStorageProvider.Default();
        Retrofit retrofit = ClientProvider.getDefaultClient().getRetrofit(mUserStorage);
        mService = retrofit.create(CasualService.class);
    }

    public Observable<BaseResult<List<ChooseGroup>>> getPersons() {
        return mService.getPersons();
    }


    public Observable<BaseRequestResult> sendCasual(String title, String content, List<Integer> ids, ArrayList<String> imgPaths) {
        final Map<String, RequestBody> imgMap = new HashMap<>();
        final List<MultipartBody.Part> partHashMap = new ArrayList<>();
        Map<String, Object> params = new HashMap<>();
        params.put("title", title);
        params.put("text", content);
        params.put("userid", ids);
        imgMap.put("data", RequestBody.create(MediaType.parse("multipart/form-data"), JSON.toJSONString(params)));

        return Observable.from(imgPaths)
                .subscribeOn(Schedulers.io())
                .flatMap(new Func1<String, Observable<File>>() {
                    @Override
                    public Observable<File> call(String path) {
                        return Luban.get(mContext)
                                .load(new File(path))
                                .putGear(Luban.THIRD_GEAR)
                                .asObservable();
                    }
                })
                .map(new Func1<File, MultipartBody.Part>() {
                    @Override
                    public MultipartBody.Part call(File file) {
                        RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
                        MultipartBody.Part body = MultipartBody.Part.createFormData("image[]", file.getName(), requestFile);
                        return body;
                    }
                })
                .toList()
                .flatMap(new Func1<List<MultipartBody.Part>, Observable<BaseRequestResult>>() {
                    @Override
                    public Observable<BaseRequestResult> call(List<MultipartBody.Part> parts) {
                        partHashMap.addAll(parts);
                        return mService.sendCasual(imgMap, partHashMap);
                    }
                });
    }








}
